package com.xz.stu.server.controller;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import com.xz.stu.server.entity.Department;
import com.xz.stu.server.service.DepartmentService;
import com.xz.stu.server.vo.department.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 院系管理")
@RestController
@RequestMapping("admin-api/school/department")
@Validated
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    @PostMapping("/create")
    @Operation(summary = "创建院系")
    public CommonResult<Long> createDepartment(@Valid @RequestBody DepartmentCreateReqVO createReqVO) {
        return success(departmentService.createDepartment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新院系")
    public CommonResult<Boolean> updateDepartment(@Valid @RequestBody DepartmentUpdateReqVO updateReqVO) {
        departmentService.updateDepartment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除院系")
    @Parameter(name = "id", description = "院系编号", required = true, example = "1024")
    public CommonResult<Boolean> deleteDepartment(@PathVariable("id") List<Long> id) {
        departmentService.deleteDepartment(id);
        return success(true);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "获得院系详情")
    @Parameter(name = "id", description = "院系编号", required = true, example = "1024")
    public CommonResult<DepartmentRespVO> getDepartment(@PathVariable("id") Long id) {
        return success(departmentService.getDepartment(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得院系分页")
    public CommonResult<PageResult<DepartmentRespVO>> getDepartmentPage(@Valid DepartmentPageReqVO pageVO) {
        return success(departmentService.getDepartmentPage(pageVO));
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取院系精简信息列表", description = "只包含被开启的院系，主要用于前端的下拉选项")
    public CommonResult<List<DepartmentSimpleRespVO>> getSimpleDepartments() {
        return success( departmentService.getSimpleDepartments());
    }
}
